package com.rchat.pocmini.common.update;

import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.aso.common.http.utils.HttpUtils;
import com.itsdf07.alog.ALog;

import java.io.File;

import okhttp3.Call;

/**
 * @Description ：App包下载器，主要用于处理下载过程中的情况，比如开始下载、下载结束或者下载过程中出现的异常
 * 只负责下载，其他比如文件的MD5校验往上抛出去处理，不在这边处理
 * @Author itsdf07
 * @Time 2017/10/17
 */

public class AppDownLoader {
    private final String TAG = AppDownLoader.class.getSimpleName();
    private static AppDownLoader mInstance;

    public interface IDownLoaderCallback {
        /**
         * 下载器开始下载
         */
        void onStartDownLoader();

        /**
         * 文件下载中
         *
         * @param currentTotalLen 已下载的大小
         * @param totalLen        下载文件的总大小
         */
        void onDownLoading(long currentTotalLen, long totalLen);

        /**
         * 下载结束，往上抛出下载好的文件
         *
         * @param file
         */
        void onFinishDownLoaded(File file);

        /**
         * 下载过程中的异常情况
         *
         * @param errCode
         * @param err
         */
        void onErrorDownLoaded(String errCode, String err);
    }

    public static AppDownLoader getInstance() {
        if (mInstance == null) {
            mInstance = new AppDownLoader();
        }
        return mInstance;
    }

    /**
     * 下载器开始下载Apk文件
     *
     * @param url          文件下载地址
     * @param destFileDir  文件存储路径【目录】
     * @param destFileName 存储文件的文件名
     * @param callback
     */
    public Call startDownLoad(String url, final String destFileDir, final String destFileName, final IDownLoaderCallback callback) {
        ALog.dTag(TAG, "url = %s,\ndestFileDir = %s,destFileName = %s", url, destFileDir, destFileName);
        return HttpUtils.downloadAsynFile(url, destFileDir, destFileName, new HttpCallbackImpl() {
            @Override
            public void onStart() {
                ALog.dTag(TAG, "%s 执行onStart...", TAG);
                if (callback != null) {
                    callback.onStartDownLoader();
                }
            }

            @Override
            public void onSuccess(BaseBean bean) {

            }

            @Override
            public void onProgress(long currentTotalLen, long totalLen) {
                ALog.dTag(TAG, "totalLen = %s,currentTotalLen = %s", totalLen, currentTotalLen);
                if (callback != null) {
//                    currentTotalLen = (currentTotalLen * 100) / totalLen;
                    callback.onDownLoading(currentTotalLen, totalLen);
                }
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (callback != null) {
                    callback.onErrorDownLoaded(bean.getCode(), bean.getDesc());
                }
            }

            @Override
            public void onFinish() {
                ALog.dTag(TAG, "%s 执行onFinish...", TAG);
                if (callback != null) {
                    File apkFile = new File(destFileDir + destFileName);
                    callback.onFinishDownLoaded(apkFile);
                }
            }
        });

    }

    public void stopDownLoad() {

    }
}
